/// <summary>
// DBLinq.MySqlProviders - .NET Providers for MySql using the framework DBLinq under MIT license, author Jiri George Moudry
// http://linq.to/db
// and the CodeFx.MySqlProviders under GNU licence, author Laurent Morisseau
// http://www.codeplex.com/CodeFxMySqlProvider
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>

namespace DBLinq.MySqlProviders
{
	using System.Collections.Specialized;
	using System.Linq;
	using System.Collections.Generic;

	internal static partial class AspNetRolesDTO
	{
		internal static void Insert( AspNetRole role )
		{
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				context.AspNetRoles.Add ( role );
				context.SubmitChanges ();
			}
		}


		internal static StringCollection UsersInRoles_GetRolesForUser( uint ApplicationId, uint UserId )
		{
			StringCollection al = null;
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				var roles= from r in context.AspNetRoles
					     join ur in context.AspNetUsersInRoles on r.RoleId equals ur.RoleId
					     where
						     ur.UserId == UserId && r.ApplicationId == ApplicationId
					     orderby r.RoleName
					     select r.RoleName;
				if ( roles != null )
				{
					al = new StringCollection ();
					foreach ( var role in roles )
					{
						al.Add ( role );
					}
				}
			}
			return al;
		}

		internal static void DeleteByFk_ApplicationId_RoleId( uint ApplicationId, uint RoleId )
		{

			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				var q = context.AspNetRoles.Where<AspNetRole> ( p => p.RoleId == RoleId && p.ApplicationId == ApplicationId ).ToList();
                if (q.Count > 0)
                {
                    AspNetRole role = q.First<AspNetRole>();
                    context.AspNetRoles.Remove(role);
                    context.SubmitChanges();
                }
			}
		}

		internal static uint GetRoleId( string _RoleName, uint _ApplicationId )
		{

			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
                var q = context.AspNetRoles.Where<AspNetRole>(p => p.RoleName == _RoleName && p.ApplicationId == _ApplicationId).ToList();
                if (q.Count > 0)
                    return q.First().RoleId;
                else
                    return 0;
			}
		}

		internal static bool Exist( string RoleName )
		{

			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				return context.AspNetRoles.Where <AspNetRole> ( p => p.RoleName == RoleName ).ToList().Count>0;
			}
		}

		internal static IEnumerable<AspNetRole> SelectByFk_ApplicationId( uint ApplicationID )
		{
			using ( aspnetcontext context = new aspnetcontext ( ConnectionString.Current.CurrentString ) )
			{
				return from r in context.AspNetRoles
					where r.ApplicationId == ApplicationID 
						select r;
			}
		}

	}
}